package com.media.publish

import android.view.View
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.bean.SharePosterBean
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ScreenUtils
import com.baselibrary.utils.StorageUtils
import com.baselibrary.utils.UMShareUtils
import com.media.R
import com.media.databinding.ActivitySharePosterSelectBinding
import com.media.publish.adapter.SharePosterPagerAdapter
import com.media.publish.utils.ScalePageTransformer
import com.umeng.socialize.bean.SHARE_MEDIA

/**
 *@author:hanke
 *2022/6/16
 *desc:
 */
@Route(path = ActivityRouter.ACTIVITY.SHARE_POSTER_SELECT)
class SharePosterSelectActivity : BaseActivity(), View.OnClickListener {
    private lateinit var binding: ActivitySharePosterSelectBinding
    private lateinit var sharePosterBean: SharePosterBean
    private var selectedPicturePath = ""
    override fun getLayoutId(): Int {
        return R.layout.activity_share_poster_select
    }

    override fun init() {
        binding = getDataBinding()
        if (intent.hasExtra(Constants.SHARE_POSTER_BEAN)) {
            sharePosterBean =
                    intent.getSerializableExtra(Constants.SHARE_POSTER_BEAN) as SharePosterBean
        }
        binding.vpPoster.pageMargin = ScreenUtils.dpToPx(8)
        val transformer = ScalePageTransformer(true)
        binding.vpPoster.setPageTransformer(true, transformer)
        binding.vpPoster.offscreenPageLimit = 3
        binding.vpPoster.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                selectedPicturePath = sharePosterBean.imagePaths[position]
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
        val sharePosterPagerAdapter = SharePosterPagerAdapter(this)
        if (sharePosterBean != null) {
            var tempList = ArrayList<String>()
            for (imagePath in sharePosterBean.imagePaths) {
                if (imagePath.endsWith("share_poster_one.jpeg")) {
                    tempList.add(imagePath)
                    break
                }
            }
            for (imagePath in sharePosterBean.imagePaths) {
                if (imagePath.endsWith("share_poster_two.jpeg")) {
                    tempList.add(imagePath)
                    break
                }
            }
            for (imagePath in sharePosterBean.imagePaths) {
                if (imagePath.endsWith("share_poster_three.jpeg")) {
                    tempList.add(imagePath)
                    break
                }
            }
            sharePosterBean.imagePaths = tempList
            sharePosterPagerAdapter.setData(sharePosterBean.imagePaths)
            binding.vpPoster.adapter = sharePosterPagerAdapter
            binding.vpPoster.currentItem = 0
            selectedPicturePath = sharePosterBean.imagePaths[0]
        }
        binding.ivSharePosterBack.setOnClickListener(this)
        binding.tvShareToWechat.setOnClickListener(this)
        binding.tvShareToFriends.setOnClickListener(this)
        binding.tvSavePicture.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.iv_share_poster_back -> finish()
            R.id.tv_share_to_wechat -> UMShareUtils.shareImageLocalFile(this, SHARE_MEDIA.WEIXIN, selectedPicturePath)
            R.id.tv_share_to_friends -> UMShareUtils.shareImageLocalFile(this, SHARE_MEDIA.WEIXIN_CIRCLE, selectedPicturePath)
            R.id.tv_save_picture -> StorageUtils.saveImageToGallery(this, selectedPicturePath)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        StorageUtils.deleteTempFile()
    }

}